package cn.hl.controller;

import cn.hl.dto.MovieTicketDto;
import cn.hl.feign.MovieServer;
import cn.hl.result.JSONResult;
import cn.hl.sentinel.UserFallBack;
import cn.hl.service.IUserService;
import cn.hl.domain.User;
import cn.hl.query.UserQuery;
import cn.hl.result.PageList;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    public IUserService userService;

    @Autowired
    private MovieServer movieServer;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    @SentinelResource(value = "user-server",fallback = "saveOrUpdate",fallbackClass = UserFallBack.class)
    public JSONResult saveOrUpdate(@RequestBody User user){
        if(user.getId()!=null){
            userService.updateById(user);
        }else{
            userService.insert(user);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    @SentinelResource(value = "user-server",fallback = "delete",fallbackClass = UserFallBack.class)
    public JSONResult delete(@PathVariable("id") Long id){
        userService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @SentinelResource(value = "user-server",fallback = "get",fallbackClass = UserFallBack.class)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(userService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @SentinelResource(value = "user-server",fallback = "list",fallbackClass = UserFallBack.class)
    public JSONResult list(){
        return JSONResult.success(userService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    @SentinelResource(value = "user-server",fallback = "page",fallbackClass = UserFallBack.class)
    public JSONResult page(@RequestBody UserQuery query){
        Page<User> page = new Page<User>(query.getPage(),query.getRows());
        page = userService.selectPage(page);
        return JSONResult.success(new PageList<User>(page.getTotal(),page.getRecords()));
    }

    /**
     * 根据Id查询电影票信息
     */
    @RequestMapping(value = "/findAllMovieTicket",method = RequestMethod.GET)
    @SentinelResource(value = "user-server",fallback = "findAllMovieTicket",fallbackClass = UserFallBack.class)
    public JSONResult findAllMovieTicket(){
        return movieServer.list();
    }

    /**
     * 根据电影Id进行购买
     */
    @RequestMapping(value="/buyMovieTicketById",method= RequestMethod.POST)
    @SentinelResource(value = "user-server",fallback = "buyMovieTicketById",fallbackClass = UserFallBack.class)
    public JSONResult buyMovieTicketById(@RequestBody MovieTicketDto movieTicketDto) {
        return userService.buyMovieTicketById(movieTicketDto);
    }

    /**
     * 根据用户Id查询用户的电影票信息
     */
    @RequestMapping(value="/findMovieTicketByUserId/{id}",method= RequestMethod.POST)
    @SentinelResource(value = "user-server",fallback = "buyMovieTicketById",fallbackClass = UserFallBack.class)
    public JSONResult findMovieTicketByUserId(@PathVariable("id") Long id) {
        return movieServer.findMovieTicketByUserId(id);
    }

    /**
     * 根据电影Id观看电影
     */
    @RequestMapping(value="/watchMovieById",method= RequestMethod.POST)
    @SentinelResource(value = "user-server",fallback = "watchMovieById",fallbackClass = UserFallBack.class)
    public JSONResult watchMovieById(@RequestBody MovieTicketDto movieTicketDto) {
        return movieServer.watchMovieById(movieTicketDto);
    }

}
